﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace LongNet.Util
{
    /// <summary>
    /// Helpers to loads the specified manifest resource from assembly.
    /// </summary>
    public class ManifestResourceHelper
    {
        #region Fields

        private Assembly assembly = null;

        /// <summary>
        /// 
        /// </summary>
        public readonly string[] ResourceNames = null;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        public ManifestResourceHelper(Assembly assembly)
        {
            this.assembly = assembly;
            ResourceNames = assembly.GetManifestResourceNames();
        }

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public Assembly OwnerAssembly
        {
            get
            {
                return this.assembly;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Stream GetStreamByFileName(string fileName)
        {
            var foundStreamName = (from f in ResourceNames where f.EndsWith(fileName) select f).FirstOrDefault();

            return this.assembly.GetManifestResourceStream(foundStreamName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public Stream GetStreamByFullName(string fullName)
        {
            return this.assembly.GetManifestResourceStream(fullName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Stream GetStreamByFileName(Assembly assembly, string fileName)
        {
            var foundStreamName = (from f in assembly.GetManifestResourceNames() where f.EndsWith(fileName) select f).FirstOrDefault();

            return assembly.GetManifestResourceStream(foundStreamName);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static Stream GetStreamByFullName(Assembly assembly, string fullName)
        {
            return assembly.GetManifestResourceStream(fullName);
        }

        #endregion
    }
}
